home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / ENGL.C < prev    next >
C/C++ Source or Header  |  1992-12-08  |  4KB  |  170 lines

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #ifdef __STDC__
  4. #include <stdarg.h>
  5. #else
  6. #include <varargs.h>
  7. #endif
  8. #include "stdpccts.h"
  9.  
  10. static void decodeType();
  11.  
  12. void show(tree)
  13. AST *tree;
  14. {
  15.     if ( tree == NULL ) return;
  16.     if ( tree->nodeType == ENode ) {
  17.         printf(" %s", zztokens[tree->data.e.token]);
  18.     } else {
  19.         english( tree );
  20.     }
  21. }
  22.  
  23. void before() { printf(" ("); }
  24. void after()  { printf(" )\n"); fflush(stdout); }
  25.  
  26. english(tree)
  27. AST *tree;
  28. {
  29.     extern int PrintEngl;
  30.  
  31.     if ( !PrintEngl ) return;
  32.     if ( tree==NULL ) return;
  33.     if ( tree->nodeType == SymQ )
  34.     {
  35.         printf("%s is", tree->data.s.name);
  36.         if ( (bottom(tree)->data.t.sc&scTypedef)!=0 )
  37.             printf(" equivalent to");
  38.         else
  39.         {
  40.             if ( (bottom(tree)->data.t.sc&scExtern)!=0 )
  41.                 printf(" globally visible and defined to be");
  42.         }
  43.         engl(tree->down, 0);
  44.         if ( tree->data.s.init!=NULL ) {
  45.             printf(" initialized to");
  46.             doExpr(tree->data.s.init);
  47.         }
  48.         printf("\n\n");
  49.         return;
  50.     }
  51.     printf("type is");
  52.     engl(tree, 0);
  53.     printf("\n\n");
  54. }
  55.  
  56. static void
  57. decodeType(base, plural)
  58. qBaseType *base;
  59. int plural;
  60. {
  61.     if ( base->sc&scAuto )    printf(" auto");
  62.     if ( base->sc&scRegister )printf(" register");
  63.     if ( base->sc&scStatic )    printf(" static");
  64.     if ( base->sc&scExtern )    ;
  65.     if ( base->sc&scTypedef )    ;
  66.  
  67.     if ( base->cv&cvConst )    printf(plural?" constant":" a constant");
  68.     if ( base->cv&cvVolatile )printf(plural?" volatile":" a volatile");
  69.  
  70.     if ( base->type&tSigned )    printf(" signed");
  71.     if ( base->type&tUnsigned)printf(" unsigned");
  72.     if ( base->type&tShort )    printf(" short");
  73.     if ( base->type&tLong )    printf(" long");
  74.     if ( base->type&tDouble )    printf(plural?" doubles":" double");
  75.     if ( base->type&tInt )     printf(plural?" ints":" int");
  76.     if ( base->type&tFloat )    printf(plural?" floats":" float");
  77.     if ( base->type&tChar )    printf(plural?" chars":" char");
  78.     if ( base->type&tVoid )    printf(plural?" objects of unspecified type":
  79.                                           " an object of unspecified type");
  80.     if ( base->type&tEllipsis)printf(" ...");
  81.     if ( base->type&tUnion )    printf(" union");
  82.     if ( base->type&tStruct )    printf(plural?" structs":" a struct");
  83.     if ( base->type&tEnum )    printf(" an enumerated type");
  84.     if ( base->type&tTypeName) printf(" a type called %s", base->name);
  85. }
  86.  
  87. engl(tree, plural)
  88. AST *tree;
  89. int plural;
  90. {
  91.     AST *a;
  92.  
  93.     if ( tree == NULL ) return;
  94.     switch ( tree->nodeType )
  95.     {
  96.         case SymQ :
  97.             printf(" symbol %s is", tree->data.s.name);
  98.             break;
  99.         case BaseTypeQ :
  100.             decodeType(&(tree->data.t), plural);
  101.             if ( (tree->data.t.type&tStruct)!=0 ||
  102.                  (tree->data.t.type&tEnum)!=0 ||
  103.                  (tree->data.t.type&tUnion)!=0 )
  104.             {
  105.                 if ( tree->data.t.name!=NULL )
  106.                     printf(" called %s",tree->data.t.name);
  107.                 if ( tree->right != NULL ) printf(" containing\n");
  108.                 for (a=tree->right; a!=NULL; a=a->right)
  109.                     {engl(a,0); putchar('\n');}
  110.             }
  111.             break;
  112.         case PointerQ :
  113.             if ( tree->data.p.cv&cvConst )
  114.                 printf(plural?" pointers to constant objects defined as":
  115.                               " a pointer to a constant object defined as");
  116.             else if ( tree->data.p.cv&cvVolatile )
  117.                 printf(plural?" pointers to volatile objects defined as":
  118.                               " a pointer to a volatile object defined as");
  119.             else
  120.                 printf(plural?" pointers to":" a pointer to");
  121.             break;
  122.         case ArrayQ :
  123.             if ( tree->data.a.dim == NULL ) {
  124.                 if (plural) printf(" undimensioned arrays of");
  125.                 else printf(" an undimensioned array of");
  126.             }
  127.             else {
  128.                 putchar(' ');
  129.                 if ( !plural ) printf("a ");
  130.                 doExpr(tree->data.a.dim);
  131.                 if (plural) printf("-element arrays of");
  132.                 else printf("-element array of");
  133.             }
  134.             plural = 1;
  135.             break;
  136.         case FunctionQ :
  137.             if ( tree->right!=NULL )
  138.             {
  139.                 printf(plural?" functions with args\n":
  140.                               " a function with args\n");
  141.                 for (a=tree->right; a!=NULL; a=a->right)
  142.                     {engl(a, 0); putchar('\n');}
  143.                 printf(" returning");
  144.             }
  145.             else
  146.                 printf(plural?" functions returning": " a function returning");
  147.             break;
  148.         case FieldQ :
  149.             printf(" field %s which is", tree->data.fi.name);
  150.             break;
  151.         case ENode :
  152.             doExpr(tree);
  153.         default :
  154.             fprintf(stderr, "illegal AST node type\n");
  155.             break;
  156.     }
  157.     engl(tree->down, plural);
  158. }
  159.  
  160. doExpr(tree)
  161. AST *tree;
  162. {
  163.     if ( tree == NULL ) return;
  164.     if ( tree->nodeType == ENode ) {
  165.         zzpre_ast(tree, show, before, after);
  166.     } else {
  167.         english( tree );
  168.     }
  169. }
  170.